package com.lzl.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzl.common_utils.JwtUtils;
import com.lzl.common_utils.MD5;
import com.lzl.educenter.entity.UcenterMember;
import com.lzl.educenter.entity.vo.RegisterVo;
import com.lzl.educenter.mapper.UcenterMemberMapper;
import com.lzl.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzl.servicebase.exceptionHandler.LZLException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-04-06
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //登录的方法
    @Override
    public String login(UcenterMember member) {
        //获取登录手机号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();

        //手机号和密码非空的判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new LZLException(20001, "登录失败");
        }

        //判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        //判断查询对象是否为空
        if (mobileMember == null) {  //没有这个手机号
            throw new LZLException(20001, "登录失败,没有这个手机号");
        }

        //判断密码  --上面已经根据手机号查到账号了
        //因为存储到数据库密码肯定是加密的
        //把输入的密码进行加密，再和数据库密码进行比较 `  加密-MD5 --只能加密，不能解密
        if (!MD5.encrypt(password).equals(mobileMember.getPassword())) {
            throw new LZLException(20001, "登录失败,密码错误");
        }
        //判断用户是否被禁用
        if (mobileMember.getIsDisabled()) {
            throw new LZLException(20001, "登录失败,用户被禁用");
        }

        //登录成功
        //生成token字符串----使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
        return jwtToken;
    }

    //注册
    @Override
    public void register(RegisterVo registerVo) {

        //为什么不用 BeanUtils.copyProperties();
        //获取注册的数据
        String code = registerVo.getCode();   //验证码
        String mobile = registerVo.getMobile();      //手机号
        String nickname = registerVo.getNickname();    //昵称
        String password = registerVo.getPassword();    //密码

        //非空判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(code) || StringUtils.isEmpty(nickname)) {
            throw new LZLException(20001, "数据不能为空，注册失败");
        }

        //判断验证码
        //因为在发送短信的方法中-是在其他服务中跑，会得到验证码保存在redis中
        String redisCode = redisTemplate.opsForValue().get(mobile);
        //如果这么写--那用户乱写 code验证码 就会报错---验证码已过期
//        if (redisCode == null) {
//            throw new LZLException(20001,"验证码已过期");
//        }
        if(!code.equals(redisCode)){
            throw new LZLException(20001,"验证码不正确");
        }

        //判断手机号是否重复，表里面存在相同手机号不进行添加
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if(count>0){
            throw new LZLException(20001,"注册失败-手机号已存在");
        }

        //数据添加数据库中
        UcenterMember member = new UcenterMember();
        //beanutil转换完 需要再把加密后密码怼进去
        member.setMobile(mobile);
        member.setNickname(nickname);
        //密码需要加密
        member.setPassword(MD5.encrypt(password));
        //用户不禁用---虽然数据库默认值就是0
        member.setIsDisabled(false);
        //设置用户默认头像
        member.setAvatar("https://edu-longyang.oss-cn-beijing.aliyuncs.com/fa104ef58c4e5bc4270d911da1d1b34d.jpg");
        //添加到数据库中
        baseMapper.insert(member);

    }

    //根据openId查询是否有数据
    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    //被service-statistics远程调用-查询某一天注册人数
    @Override
    public Integer countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }

}
